package org.vectrics.user.web.struts;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ecs.html.A;
import org.apache.log4j.Logger;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPerson;
import org.vectrics.company.CompanyPersonCriteria;
import org.vectrics.company.CompanyPersonGroup;
import org.vectrics.company.CompanyPersonGroupType;
import org.vectrics.company.CompanyPersonPageList;
import org.vectrics.company.CompanyPersonRole;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;


public class PersonCompaniesListTag extends PageListTag {
	public static final long serialVersionUID = 2435678043534L;
	private transient Logger log = Logger.getLogger(this.getClass());
	private String companyId = null;
	private String personId = null;
	
	
	public PersonCompaniesListTag() {
		super();
		this.setWidth("100%");
	}

	protected String getDescription() {
		return null;
	}

	protected String getPageListLink() {
		return null;
	}

	protected void showList(String description, Cell mainCell, List elements, Form form) {
		BorderTable table = new BorderTable();
		addHeaderRow(table);
		Iterator iter = elements.iterator();
		while (iter.hasNext()) {
			CompanyPerson companyPerson = (CompanyPerson)iter.next();
			addCompanyPersonRow(table, companyPerson);
		}
		mainCell.addElement(table);
	}

	private void addCompanyPersonRow(Table table, CompanyPerson companyPerson) {
		Company company = companyPerson.getCompany();
		//Domain domain = company.getDomain();
		//Person person = companyPerson.getPerson();
		
		Row row = table.addRow();
		row.setDefaultCellFontClass("subFont");
		Cell cell = row.addCell();
		A companyPersonLink = new A();
		companyPersonLink.setClass("subLink");
		companyPersonLink.addElement(company.getName());
		companyPersonLink.setHref(this.getContextPath() + "/admin/company/person/view.do?companyPersonId=" +
				companyPerson.getId() +
				"&companyId=" + companyPerson.getCompany().getId());
		cell.addElement(companyPersonLink);
		

		Set currentCompanyGroupTypes = new HashSet();
		Table roleInnerTable = new Table();
		Iterator iter = companyPerson.getRoles().iterator();
		while (iter.hasNext()) {
			CompanyPersonRole role = (CompanyPersonRole)iter.next();
			
			log.debug("Find group memberships from role, if role is current (endDate = null)");
			if (role.getDateEnd() == null) {
				Iterator groupIter = role.getGroups().iterator();
				while (groupIter.hasNext()) {
					CompanyPersonGroup group = (CompanyPersonGroup)groupIter.next();
					currentCompanyGroupTypes.add(group.getCompanyPersonGroupType());
				}
			}
			
			Row roleRow = roleInnerTable.addRow();
			roleRow.setDefaultCellFontClass("subFont");
			Cell titleCell = roleRow.addCell();
			titleCell.addText(role.getTitle());

			Cell dateStartCell = roleRow.addCell();
			dateStartCell.addText(CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(role.getDateStart()));

			roleRow.addCell().addText(" - ");
			Cell dateEndCell = roleRow.addCell();
			if (role.getDateEnd() == null) {
				dateEndCell.addText(this.getPhrase("phrase.present.date"));

			} else {
				dateEndCell.addText(CoreServiceLocator.getDateFormat().getDateFormatRoundToDay(this.getLocale()).format(role.getDateEnd()));
			}
		}
		
		Cell roleCell = row.addCell();
		roleCell.addElement(roleInnerTable);
		
		Table companyGroupsTable = new Table();
		companyGroupsTable.setWidth(300);
		Cell companyGroupsCell = row.addCell();
		if (currentCompanyGroupTypes.size() > 0) {
			Iterator groupIter = currentCompanyGroupTypes.iterator();
			while (groupIter.hasNext()) {
				Row privGroupsRow = companyGroupsTable.addRow(); 
				CompanyPersonGroupType groupType = (CompanyPersonGroupType)groupIter.next();
				Cell privGroupsCell = privGroupsRow.addCell();
				privGroupsCell.addText("subFont", this.getPhrase(groupType.getDescriptionCode()));
			}
			companyGroupsCell.addElement(companyGroupsTable);
		} else {
			companyGroupsCell.addElement(TextHelper.getFont("subFont", "&nbsp;"));
		}
	}

	
	private void addHeaderRow(Table table) {
		Row row = table.addRow();
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		row.setDefaultCellFontClass("tableHdrFont");
		
		if (log.isDebugEnabled())
			log.debug("addHeaderRow() - row.defaultCellFontClass=" 
					+ row.getDefaultCellFontClass() + "  Row = " + row);
		
		Cell cell = row.addCell();
		cell.setVAlign("center");
		cell.addText(this.getPhrase("prompt.company.name"));
		
		cell = row.addCell();
		cell.setVAlign("center");
		cell.addText(this.getPhrase("prompt.company.roles"));

		cell = row.addCell();
		cell.setVAlign("center");
		cell.addText(this.getPhrase("prompt.company.groups"));
	}

	
	protected PageList createPageList() {
		log.info("createPageList() - personID = " + personId);
    	CompanyPersonCriteria criteria = new CompanyPersonCriteria();
    	
		Set domains = CoreServiceLocator.getPersonService()
			.findDomainsWithUserPrivilege(this.getAuthPerson(), Company.PRIVILEGE_COMPANY_VIEW);
		criteria.setDomains(domains);
    	
    	
    	if (companyId != null)
    		criteria.setCompany(CoreServiceLocator.getCompanyService().findCompany(new Long(companyId)));
    	
    	if (personId != null)
    		criteria.setPerson(CoreServiceLocator.getPersonService().findPersonById(new Long(personId)));
    	
    	PageList pageList = new CompanyPersonPageList(criteria);
    	return(pageList);
	}

	
	/**
	 * @return Returns the companyId.
	 */
	public String getCompanyId() {
		return companyId;
	}

	/**
	 * @param companyId The companyId to set.
	 */
	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	/**
	 * @return Returns the personId.
	 */
	public String getPersonId() {
		return personId;
	}

	/**
	 * @param personId The personId to set.
	 */
	public void setPersonId(String personId) {
		this.personId = personId;
		if ((personId != null) && (personId.equals("current")))
			this.personId = ThreadProperties.getPerson().getId().toString();
	}

	
	
}
